package com.showde.shoppingmall.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import android.app.Activity;
import android.app.ActivityGroup;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;

import com.showde.shoppingmall.activity.AddressEditActivity;
import com.showde.shoppingmall.activity.AddressManagerActivity;
import com.showde.shoppingmall.activity.BaseGroupActivity;
import com.showde.shoppingmall.activity.BillListActivity;
import com.showde.shoppingmall.activity.CategoryGroup;
import com.showde.shoppingmall.activity.CustomerServiceActivity;
import com.showde.shoppingmall.activity.FeedbackActivity;
import com.showde.shoppingmall.activity.MoreGroup;
import com.showde.shoppingmall.activity.MyCollectionActivity;
import com.showde.shoppingmall.activity.MyStreetGroup;
import com.showde.shoppingmall.activity.NormalQuestionActivity;
import com.showde.shoppingmall.activity.PasswordModifyActivity;
import com.showde.shoppingmall.activity.ProductDetailsActivity;
import com.showde.shoppingmall.activity.RecommendGroup;
import com.showde.shoppingmall.activity.RegisterActivity;
import com.showde.shoppingmall.activity.ShoppingcarGroup;
import com.showde.shoppingmall.activity.ShoppingcartModifyActivity;
import com.showde.shoppingmall.activity.TabMainActivity;
import com.showde.shoppingmall.activity.UserInfoActivity;

@SuppressWarnings("deprecation")
public class ActivityDispatch {

	public static final String GROUP_TAG = "tag";

	public static final int GROUP_STREET = 0;
	public static final int GROUP_RECOMMEND = 1;
	public static final int GROUP_CATEGORY = 2;
	public static final int GROUP_SHOPPINGCART = 3;
	public static final int GROUP_MORE = 4;

	public static final String PRODUCT_RECOMMEND = "recommend";

	/**
	 * 商品类别ID
	 */
	public static final String PRODUCT_CATEGORY_ID = "productCID";
	/**
	 * 商品ID
	 */
	public static final String PRODUCT_ID = "productID";
	/**
	 * 商品id列表
	 */
	public static final String PRODUCT_ID_ARRAY = "pidArray";

	/**
	 * 收件地址
	 */
	public static final String ADDRESS_KEY = "productKey";

	public static final ActivityGroup getActivityGroup(int groupId) {
		ActivityGroup group = null;
		switch (groupId) {
		case GROUP_STREET:
			group = MyStreetGroup.group;
			break;
		case GROUP_RECOMMEND:
			group = RecommendGroup.group;
			break;
		case GROUP_CATEGORY:
			group = CategoryGroup.group;
			break;
		case GROUP_SHOPPINGCART:
			group = ShoppingcarGroup.group;
			break;
		case GROUP_MORE:
			group = MoreGroup.group;
			break;
		}
		return group;
	}

	/**
	 * 在activitygroup中打开新界面
	 * 
	 * @param theActivityFrom
	 * @param theClassToOpen
	 * @param group
	 * @param activityID
	 */
	public static void openNewActivity(Context theActivityFrom,
			Class<?> theClassToOpen, ActivityGroup group, String activityID,
			int qroupTag) {
		// 要跳转的Activity
		Intent intent = new Intent(theActivityFrom, theClassToOpen);
		intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
		intent.putExtra(GROUP_TAG, qroupTag);
		// 把Activity转换成一个Window，然后转换成View
		Window w = group.getLocalActivityManager().startActivity(
				UUID.randomUUID().toString()/* activityID */, intent);
		View view = w.getDecorView();
		// 设置要跳转的Activity显示为本ActivityGroup的内容
		((BaseGroupActivity) group).replaceView(view);
	}

	/**
	 * 在activitygroup中打开新界面
	 * 
	 * @param theActivityFrom
	 * @param theClassToOpen
	 * @param group
	 * @param activityID
	 * @param bundle
	 */
	public static void openNewActivity(Context theActivityFrom,
			Class<?> theClassToOpen, ActivityGroup group, String activityID,
			Bundle bundle, int qroupTag) {
		// 要跳转的Activity
		Intent intent = new Intent(theActivityFrom, theClassToOpen);
		intent.putExtra(GROUP_TAG, qroupTag);
		intent.putExtras(bundle);
		// 把Activity转换成一个Window，然后转换成View
		Window w = group.getLocalActivityManager().startActivity(
				UUID.randomUUID().toString(), intent);
		View view = w.getDecorView();
		// 设置要跳转的Activity显示为本ActivityGroup的内容
		((BaseGroupActivity) group).replaceView(view);
	}

	/**
	 * 返回activitygroup中上一个界面
	 */
	public static void backToAboveActivity(ActivityGroup group) {
		((BaseGroupActivity) group).back();
	}

	public static void toHome(Activity activity) {
		Intent intent = new Intent(Intent.ACTION_MAIN);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.addCategory(Intent.CATEGORY_HOME);
		activity.startActivity(intent);
	}

	/**
	 * 跳转到应用主页
	 * 
	 * @param packageContext
	 */
	public static final void toMain(Context packageContext) {
		Intent intent = new Intent();
		intent.setClass(packageContext, TabMainActivity.class);
		packageContext.startActivity(intent);
	}

	/**
	 * 跳转到商品详情页
	 * 
	 * @param packageContext
	 */
	public static final void toProductDetails(Context packageContext) {
		Intent intent = new Intent();
		intent.setClass(packageContext, ProductDetailsActivity.class);
		packageContext.startActivity(intent);
	}

	public static final void toShoppingcartModify(Context packageContext,
			ArrayList<String> checkedProductIds) {
		Bundle bundle = new Bundle();
		bundle.putSerializable(ActivityDispatch.PRODUCT_ID_ARRAY,
				checkedProductIds);
		Intent intent = new Intent();
		intent.setClass(packageContext, ShoppingcartModifyActivity.class);
		intent.putExtras(bundle);
		packageContext.startActivity(intent);
	}

	/**
	 * 跳转到用户账号信息页
	 * 
	 * @param packageContext
	 */
	public static final void toUserInfo(Context packageContext) {
		Intent intent = new Intent();
		intent.setClass(packageContext, UserInfoActivity.class);
		packageContext.startActivity(intent);
	}

	/**
	 * 进入到客户服务页面
	 * 
	 * @param packageContext
	 */
	public static final void toCustomerService(Context packageContext) {
		Intent intent = new Intent();
		intent.setClass(packageContext, CustomerServiceActivity.class);
		packageContext.startActivity(intent);
	}

	/**
	 * 进入到常见问题页面
	 * 
	 * @param packageContext
	 */
	public static final void toNormalQuestion(Context packageContext) {
		Intent intent = new Intent();
		intent.setClass(packageContext, NormalQuestionActivity.class);
		packageContext.startActivity(intent);
	}

	/**
	 * 进入到常见问题页面
	 * 
	 * @param packageContext
	 */
	public static final void toSettings(Context packageContext) {
		// Intent intent = new Intent();
		// intent.setClass(packageContext, NormalQuestionActivity.class);
		// packageContext.startActivity(intent);
	}

	/**
	 * 进入到留言板页面
	 * 
	 * @param packageContext
	 */
	public static final void toFeedback(Context packageContext) {
		Intent intent = new Intent();
		intent.setClass(packageContext, FeedbackActivity.class);
		packageContext.startActivity(intent);
	}

	/**
	 * 进入到收件人地址管理页面
	 * 
	 * @param packageContext
	 */
	public static final void toAddressManager(Context packageContext) {
		Intent intent = new Intent();
		intent.setClass(packageContext, AddressManagerActivity.class);
		packageContext.startActivity(intent);
	}

	/**
	 * 进入到收件人地址编辑页面
	 * 
	 * @param packageContext
	 */
	public static final void toAddressEdit(Context packageContext) {
		Intent intent = new Intent();
		intent.setClass(packageContext, AddressEditActivity.class);
		packageContext.startActivity(intent);
	}

	/**
	 * 进入到收件人地址编辑页面
	 * 
	 * @param packageContext
	 */
	public static final void toBillList(Context packageContext) {
		Intent intent = new Intent();
		intent.setClass(packageContext, BillListActivity.class);
		packageContext.startActivity(intent);
	}

	/**
	 * 进入到我的收藏页面
	 * 
	 * @param packageContext
	 */
	public static final void toMyCollection(Context packageContext) {
		Intent intent = new Intent();
		intent.setClass(packageContext, MyCollectionActivity.class);
		packageContext.startActivity(intent);
	}

	/**
	 * 跳转到注册页面
	 * 
	 * @param packageContext
	 */
	public static final void toRegister(Context packageContext) {
		Intent intent = new Intent();
		intent.setClass(packageContext, RegisterActivity.class);
		packageContext.startActivity(intent);
	}

	/**
	 * 跳转到修改密码页面
	 * 
	 * @param packageContext
	 */
	public static final void toModifyPassword(Context packageContext) {
		Intent intent = new Intent();
		intent.setClass(packageContext, PasswordModifyActivity.class);
		packageContext.startActivity(intent);
	}

}
